﻿using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Janus.Cores.Logger
{
    /// <summary>
    /// <para>功能：Log4Net 的日志实现</para>
    /// <para>作者：luochengbang@163.com </para>
    /// <para>日期：2015/1/6 14:16:19 </para>
    /// <para>备注：本代码版权归Janus所有，严禁外传 </para>
    /// </summary>
    public class Log4NetLogger : ILogger
    {
        /// <summary>
        /// log4net
        /// </summary>
        private readonly ILog _log;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="log">log4net</param>
        public Log4NetLogger(ILog log)
        {
            this._log = log;
        }

        #region ILogger Members

        /// <summary>
        /// 写入一条 debug 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        public void Debug(object message)
        {
            this._log.Debug(message);
        }

        /// <summary>
        /// 写入一条 debug 等级的消息
        /// </summary>
        /// <param name="format">格式</param>
        /// <param name="args">参数</param>
        public void DebugFormat(string format, params object[] args)
        {
            this._log.DebugFormat(format, args);
        }

        /// <summary>
        /// 写入一条 debug 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        /// <param name="exception">异常</param>
        public void Debug(object message, Exception exception)
        {
            this._log.Debug(message, exception);
            if (exception.InnerException != null)
            {
                this.Debug(exception.InnerException.Message, exception.InnerException);
            }
        }

        /// <summary>
        /// 写入一条 info 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        public void Info(object message)
        {
            this._log.Info(message);
        }

        /// <summary>
        /// 写入一条 info 等级的消息
        /// </summary>
        /// <param name="format">格式</param>
        /// <param name="args">参数</param>
        public void InfoFormat(string format, params object[] args)
        {
            this._log.InfoFormat(format, args);
        }

        /// <summary>
        /// 写入一条 info 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        /// <param name="exception">异常</param>
        public void Info(object message, Exception exception)
        {
            this._log.Info(message, exception);
            if (exception.InnerException != null)
            {
                this.Info(exception.InnerException.Message, exception.InnerException);
            }
        }

        /// <summary>
        /// 写入一条 error 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        public void Error(object message)
        {
            this._log.Error(message);
#if DEBUG
            throw message as Exception;
#endif
        }

        /// <summary>
        /// 写入一条 error 等级的消息
        /// </summary>
        /// <param name="format">格式</param>
        /// <param name="args">参数</param>
        public void ErrorFormat(string format, params object[] args)
        {
            this._log.ErrorFormat(format, args);
        }

        /// <summary>
        /// 写入一条 error 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        /// <param name="exception">异常</param>
        public void Error(object message, Exception exception)
        {
            this._log.Error(message, exception);
            if (exception.InnerException != null)
            {
                this.Error(exception.InnerException.Message, exception.InnerException);
            }
        }

        /// <summary>
        /// 写入一条 warnning 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        public void Warn(object message)
        {
            this._log.Warn(message);
        }

        /// <summary>
        /// 写入一条 warnning 等级的消息
        /// </summary>
        /// <param name="format">格式</param>
        /// <param name="args">参数</param>
        public void WarnFormat(string format, params object[] args)
        {
            this._log.WarnFormat(format, args);
        }

        /// <summary>
        /// 写入一条 warnning 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        /// <param name="exception">异常</param>
        public void Warn(object message, Exception exception)
        {
            this._log.Warn(message, exception);
            if (exception.InnerException != null)
            {
                this.Warn(exception.InnerException.Message, exception.InnerException);
            }
        }

        /// <summary>
        /// 写入一条 fatal 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        public void Fatal(object message)
        {
            this._log.Fatal(message);
        }

        /// <summary>
        /// 写入一条 fatal 等级的消息
        /// </summary>
        /// <param name="format">格式</param>
        /// <param name="args">参数</param>
        public void FatalFormat(string format, params object[] args)
        {
            this._log.FatalFormat(format, args);
        }

        /// <summary>
        /// 写入一条 fatal 等级的消息
        /// </summary>
        /// <param name="message">待写入的消息</param>
        /// <param name="exception">异常</param>
        public void Fatal(object message, Exception exception)
        {
            this._log.Fatal(message, exception);
            if (exception.InnerException != null)
            {
                this.Fatal(exception.InnerException.Message, exception.InnerException);
            }
        }

#endregion
    }
}
